<template>
  <div class="system-configs">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>系统配置</span>
        </div>
      </template>
      
      <el-table :data="configs" style="width: 100%" v-loading="loading">
        <el-table-column prop="config_key" label="配置键" />
        <el-table-column prop="config_value" label="配置值" />
        <el-table-column prop="config_type" label="类型" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="is_public" label="是否公开">
          <template #default="{ row }">
            <el-tag :type="row.is_public ? 'success' : 'info'" size="small">
              {{ row.is_public ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="editConfig(row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
  
  <!-- 编辑配置对话框 -->
  <el-dialog v-model="dialogVisible" title="编辑系统配置" width="600px">
    <el-form :model="editForm" label-width="120px">
      <el-form-item label="配置键">
        <el-input v-model="editForm.config_key" disabled />
      </el-form-item>
      <el-form-item label="配置值">
        <el-input v-model="editForm.config_value" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="editForm.config_type" disabled />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="editForm.description" />
      </el-form-item>
      <el-form-item label="是否公开">
        <el-switch v-model="editForm.is_public" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveConfig">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { SystemConfig } from '@/types'
import { systemApi } from '@/api/system'
import { ElMessage } from 'element-plus'

const configs = ref<SystemConfig[]>([])
const loading = ref(false)

const dialogVisible = ref(false)
const editForm = ref<Partial<SystemConfig>>({})

const loadConfigs = async () => {
  loading.value = true
  try {
    const res = await systemApi.getSystemConfigs({ page: 1, size: 100 })
    configs.value = res.data.items as unknown as SystemConfig[]
  } catch (e) {
    ElMessage.error('加载系统配置失败')
  } finally {
    loading.value = false
  }
}

const editConfig = (config: SystemConfig) => {
  editForm.value = { ...config }
  dialogVisible.value = true
}

const saveConfig = async () => {
  try {
    await systemApi.updateSystemConfig(editForm.value.config_key!, editForm.value)
    ElMessage.success('保存成功')
    dialogVisible.value = false
    loadConfigs()
  } catch (e) {
    ElMessage.error('保存失败')
  }
}

onMounted(loadConfigs)
</script>

<style scoped>
.system-configs {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 